package com.it.controller;


import com.it.entity.user.AiStaffUser;
import com.it.service.AiStaffUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.R;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公司工作人员信息表（含职位信息，供业务表关联） 前端控制器
 * </p>
 *
 * @author lutao
 * @since 2025年09月17日
 */
@RestController
@RequestMapping("/aiStaffUser")
public class AiStaffUserController {

    @Autowired
    private AiStaffUserService aiStaffUserService;

    @GetMapping("/list")
    public R list(@RequestParam(value = "pageNum" ,defaultValue = "1",required = false) Integer pageNum,
                  @RequestParam(value = "pageSize",defaultValue = "5",required = false)  Integer pageSize,
                  @RequestParam(value = "workNo",required = false) String workNo,
                  @RequestParam(value = "realName",required = false) String realName,
                  @RequestParam(value = "positionCode",required = false) String positionCode) {

        return aiStaffUserService.list(pageNum,pageSize,workNo,realName,positionCode);
    }

    /**
     * 新增
     */
    @PostMapping
    public R save(@RequestBody AiStaffUser aiStaffUser) {
        aiStaffUser.setCreateTime(LocalDateTime.now());
        return aiStaffUserService.save(aiStaffUser)  ? R.Success("新增成功",null) : R.Failed("新增失败");
    }

    /**
     * 更新
     */
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Integer id,@RequestBody AiStaffUser aiStaffUser){
        aiStaffUser.setStaffId(id);
        aiStaffUser.setUpdateTime(LocalDateTime.now());
        return aiStaffUserService.updateById(aiStaffUser) ? R.Success("更新成功",null) : R.Failed("更新失败");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id){
        return aiStaffUserService.removeById(id) ? R.Success("删除成功",null) : R.Failed("删除失败");
    }

    @GetMapping("/export")
    public void exportExcel(@RequestParam(value = "pageNum", defaultValue = "1",required = false) int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "5",required = false) int pageSize,
                            @RequestParam(value = "workNo",required = false) String workNo,
                            @RequestParam(value = "realName",required = false) String realName,
                            @RequestParam(value = "positionCode",required = false) String positionCode,
                            HttpServletResponse response) {
        aiStaffUserService.exportExcel(pageNum,pageSize,workNo,realName,positionCode,response);
    }

    /**
     *  查询对应职位的员工
     *
     *  */
    @GetMapping("/worker")
    public R workers(@RequestParam("positionCode") String positionCode) {
        return aiStaffUserService.workers(positionCode);
    }

}
